﻿using RockBus.Monitoring.DataContracts.Configuration;
using RockBus.Monitoring.DataContracts;
using System;
using System.Diagnostics;

namespace RockBus.Monitoring
{
    /// <summary>
    /// Collects the performance counter instance value.
    /// </summary>
    internal class PerformanceCounterInstanceCollector : PerformanceCounterCollectorBase
    {
        private readonly System.Diagnostics.PerformanceCounter _performanceCounterInstance;

        public PerformanceCounterInstanceCollector(PerformanceCounterInstanceConfiguration pcc, int bufferSize)
            : base(pcc, bufferSize)
        {
            this.InstanceName = pcc.InstanceName;
            this._performanceCounterInstance = new System.Diagnostics.PerformanceCounter(pcc.CategoryName, pcc.CounterName, pcc.InstanceName, true);
        }

        public string InstanceName { get; private set; }

        /// <summary>
        /// Collects the performance counter instance value.
        /// </summary>
        public override void Collect()
        {
            if (this.PerformanceCounterConfigurationBase.CounterExists())
            {
                var counterSample = this._performanceCounterInstance.NextSample();
                this.AddCounterSample(this.PerformanceCounterConfigurationBase.CounterName, this.InstanceName, counterSample);
            }
        }
    }
}